<?php

function get_rank($group, $points = NULL) {
	global $conf, $lang;

	SQLvalidate($group);
	$db=new dbquery;

	if($group) {
		$db->cache('group_rank_'.$group, 'groups');
		$db->query("SELECT rank FROM $conf[prefix_groups]groups WHERE id = $group") or $db->err(__FILE__, __LINE__);

		if($db->num_rows(false, true) == 0)
		return false;
		else {
			$gr = $db->fetch_object(true);
			if($gr->rank != 0)
			$rank = $gr->rank;
		}
	}

	if(is_numeric($rank)) {
		$db->cache('rank_'.$rank, 'ranks');
		$db->query("SELECT * FROM $conf[prefix_ranks]ranks WHERE id = $rank") or $db->err(__FILE__, __LINE__);

		if($db->num_rows(false, true) == 0)
		return false;
		else {
			$r = $db->fetch_object(true);
			$r->path = $conf['images_dir'].$r->path;

			$nm = "name_$_SESSION[lang_short]";

			if($r->$nm == '') {
				$nm = "name_".$conf['language'];
			}

			$name = read_text_rest($r->$nm);

			return array('path' => $r->path, 'name' => $name);
		}
	}
	elseif(is_numeric($points)) {
		$db->cache('ranks', 'ranks');
		$db->query("SELECT * FROM $conf[prefix_ranks]ranks ORDER BY points DESC") or $db->err(__FILE__, __LINE__);
		
		if($db->num_rows(false, true) == 0)
			return false;
		else {
			$rnk=NULL;
			while($r=$db->fetch_object()) {
				if($r->points<=$points && empty($rnk)) $rnk=$r;
			}
			
			$rnk->path = $conf['images_dir'].$rnk->path;

			$nm = "name_$_SESSION[lang_short]";
			if($rnk->$nm == '') $nm = "name_".$conf['language'];
			$name = read_text_rest($rnk->$nm);

			return array('path' => $rnk->path, 'name' => $name);
		}
	}
	else
	return false;
}
?>
